#!/bin/bash
#
# Install trac from yum repository and create user.
#

############################################################
# Plugin :  AccountManager
############################################################
function install_plugin_actmgr() {
    echo "Installing plugin : accountmanagerplugin v0.11"
    svn co https://trac-hacks.org/svn/accountmanagerplugin/0.11 $PLUGIN_HOME/accountmanagerplugin-0.11
    if [ ! $? -eq 0 ];
    then
        echo "Error : Checking out plugin source from https://trac-hacks.org/svn/accountmanagerplugin/0.11"
        echo "AccountManager plugin not installed"
    else
        cd $PLUGIN_HOME/accountmanagerplugin-0.11
        
        echo "Building AccountManager plugin (egg file)"
        python ./setup.py bdist_egg
        
        echo "Copying AccountManager egg to $TRAC_HOME/plugins"
        cp dist/ $TRAC_HOME/plugins/.
    fi
}

echo "================================================================="
echo "Install trac ($SERVER_NAME): begin"

TRAC_HOME=/$SERVER_NAME/trac

source $COMMON_FUNC > /dev/null 2>&1
if [ $? -gt 0 ]; then
    echo "Error: This script requires common-func.bash"
    exit 1
fi

isRoot

echo "Installing trac software from yum repository"
yum install -y trac mod_python mod_dav mod_dav_svn MySQL-python

# creating system user/group for trac
user_exists trac
if [ $? -eq 1 ]; then
    echo "Creating system user trac"
    useradd -r trac
else
    echo "System user trac already exists, nothing to do"
fi

echo "Creating home directory for trac"
mkdir -p $TRAC_HOME/etc
mkdir -p $TRAC_HOME/etc/3p
mkdir -p $TRAC_HOME/etc/plugins
mkdir -p $TRAC_HOME/projects

prompt INPUT "Do you want to install the recommended trac plugin(s)? (y|n)
AccountManager : " "^[yYnN]$"

if [[ $INPUT =~ ^[yY]$ ]];
then
    echo "Copying over trac resources"
    cp $RESOURCES_DIR/trac/trac.ini.global $TRAC_HOME/etc/trac.ini
    
    which svn > /dev/null 2>&1
    if [ $? -eq 0 ];
    then
        echo "Performing trac installation of plugin(s)"
        PLUGIN_HOME=$TRAC_HOME/etc/3p
        
        install_plugin_actmgr
        
        echo "Applying permissions to $TRAC_HOME/plugins"
        chown -R trac:trac $TRAC_HOME/plugins
        chmod -R 770 $TRAC_HOME/plugins
    else 
        echo "Error: Installing plugins requires subversion. No 'svn' command found."
        echo "By-passing plugin installations"
    fi
fi

echo "Creating root link (/trac) to $TRAC_HOME"
ln -s $TRAC_HOME /trac

echo "Applying permissions for trac"
chown -R trac:trac $TRAC_HOME
chmod -R 770 $TRAC_HOME
chown trac:$SERVER_NAME /trac
chmod 770 /trac

echo "Creating iptables for trac (port 3000)"
open_port 3000
if [ ! $? -eq 0 ];
then
    echo "Encountered error during open_port 3000"
    pause '> Press [ENTER] to continue or [CTRL-C] to exit'
fi

echo "Install trac: done"
echo "================================================================="
